package request

import (
	"github.com/astaxie/beego/validation"
	"shop-common/utils/regular"
)

type SysUserInsertRequest struct {
	Username       string `json:"username" binding:"required"`           //用户名
	Email          string `json:"email" binding:"required"`              //邮箱
	Mobile         string `json:"mobile" binding:"required"`             //手机号
	Password       string `json:"password" binding:"required"`           //设置密码
	RepeatPassword string `json:"repeatPassword" binding:"required"`     //确认密码
	RoleIdList     []uint `json:"roleIdList"`                            //角色列表rid
	Status         int    `json:"status" binding:"required" default:"2"` //状态  1:停用  2:启用
}

func (s *SysUserInsertRequest) Valid(v *validation.Validation) {
	if s.Username == "" {
		v.SetError("Username", "用户名不能为空")
	} else if !regular.IsUsername(s.Username) {
		v.SetError("Username", "用户名必须由5-12位(数字、字母、_、-)组成，且必须以字母开头")
	} else if !regular.IsEmail(s.Email) {
		v.SetError("Email", "邮箱格式不符合规范")
	} else if !regular.IsMobile(s.Mobile) {
		v.SetError("Mobile", "手机号格式不符合规范")
	} else if len(s.Password) < 6 {
		v.SetError("Password", "密码至少需要6位")
	} else if s.Password != s.RepeatPassword {
		v.SetError("RepeatPassword", "两次输入密码不匹配")
	} else if s.Status < 1 {
		v.SetError("Status", "状态不符合规范")
	}
}

type SysUserUpdateRequest struct {
	UserId         string `json:"userId" binding:"required"`
	Username       string `json:"username" binding:"required"`
	Password       string `json:"password"`
	RepeatPassword string `json:"repeatPassword"`
	RoleIdList     []uint `json:"roleIdList"`
	Status         int    `json:"status" binding:"required" default:"2"`
}

func (s *SysUserUpdateRequest) Valid(v *validation.Validation) {
	if s.Username == "" {
		v.SetError("Username", "用户名不能为空")
	} else if !regular.IsUsername(s.Username) {
		v.SetError("Username", "用户名必须由5-12位(数字、字母、_、-)组成，且必须以字母开头")
	} else if len(s.Password) < 6 && s.Password != "" {
		v.SetError("Password", "密码至少需要6位")
	} else if s.Password != s.RepeatPassword {
		v.SetError("RepeatPassword", "两次输入密码不匹配")
	} else if s.Status < 1 {
		v.SetError("Status", "状态不符合规范")
	}
}

type UpdateSysUserStatusRequest struct {
	UserId string `json:"userId" binding:"required"`             //用户ID
	Status int    `json:"status" binding:"required" default:"2"` //状态  1:停用  2:启用
}

func (s *UpdateSysUserStatusRequest) Valid(v *validation.Validation) {
	if s.UserId == "" {
		v.SetError("Uid", "uid不能为空")
	}
}

type SysUserLoginRequest struct {
	Username  string `json:"username" binding:"required"` //用户名
	Password  string `json:"password" binding:"required"` //密码
	Captcha   string `form:"captcha"`                     //验证码
	CaptchaId string `form:"captchaId"`                   //captchaId
}

func (s *SysUserLoginRequest) Valid(v *validation.Validation) {
	if s.Username == "" {
		v.SetError("Username", "用户名不能为空")
	} else if !regular.IsUsername(s.Username) {
		v.SetError("Username", "用户名必须由5-12位(数字、字母、_、-)组成，且必须以字母开头")
	} else if len(s.Password) < 6 {
		v.SetError("Password", "密码至少需要6位")
	}
}

type RefreshRequest struct {
	AccessToken string `json:"accessToken"` //旧token
}

func (r *RefreshRequest) Valid(v *validation.Validation) {
	if r.AccessToken == "" {
		v.SetError("AccessToken", "token不能为空")
	}
}

type PasswordRequest struct {
	Password          string `json:"password" binding:"required"`          //旧密码
	NewPassword       string `json:"newPassword" binding:"required"`       //新密码
	NewRepeatPassword string `json:"newRepeatPassword" binding:"required"` //确认密码
}

func (p *PasswordRequest) Valid(v *validation.Validation) {
	if p.Password == "" || len(p.Password) < 6 {
		v.SetError("Password", "密码不能为空")
	} else if len(p.NewPassword) < 6 {
		v.SetError("NewPassword", "密码至少需要6位")
	} else if p.NewPassword != p.NewRepeatPassword {
		v.SetError("NewRepeatPassword", "两次输入密码不匹配")
	}
}
